package com.zbkj.admin.controller;

import com.zbkj.common.model.bte.CallBackEntity;
import com.zbkj.common.utils.JsonUtils;
import com.zbkj.service.service.CallbackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * 支付回调
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2020 <a href="https://www.crmeb.com">...</a> All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/payment/callback")
@Api(tags = "支付回调")
@RequiredArgsConstructor
public class CallbackController {


    private final CallbackService callbackService;

    /**
     * 微信支付回调
     */
    @ApiOperation(value = "微信支付回调")
    @RequestMapping(value = "/wechat", method = RequestMethod.POST)
    public String weChat(@RequestBody String  request) {
        System.out.println("微信支付回调 request ===> " + request);
        String response = callbackService.weChat(request);
        System.out.println("微信支付回调 response ===> " + response);
        return response;
    }

    @ApiOperation("银企支付回调")
    @PostMapping("/bte")
    public String bte(HttpServletRequest request) throws IOException {

        CallBackEntity callBack = getPostByTextPlain(request);
        log.info("银企支付的回调 请求: {}", callBack);

        String response = callbackService.bte(callBack);
        System.out.println("银企支付回调 response ===> " + response);
//        return null;
        return response;
    }

    /**
     * 微信退款回调
     */
    @ApiOperation(value = "微信退款回调")
    @RequestMapping(value = "/wechat/refund", method = RequestMethod.POST)
    public String weChatRefund(@RequestBody String request) {
        System.out.println("微信退款回调 request ===> " + request);
        String response = callbackService.weChatRefund(request);
        System.out.println("微信退款回调 response ===> " + response);
        return response;
    }

    @ApiOperation("银企退款回调")
    @PostMapping("/bte/refund")
    public String bteRefund(HttpServletRequest request) throws IOException {

        CallBackEntity callBack = getPostByTextPlain(request);

        String response = callbackService.bteRefund(callBack);
        log.info("银企退款通知确认结果: ====>" + response);
        return response;
    }

    /**
     * 获取 request 中用POST方式"Content-type"是"text/plain"发送的 json数据
     */
    public static CallBackEntity getPostByTextPlain(HttpServletRequest request) throws IOException {

        BufferedReader reader = request.getReader();
        char[] buf = new char[512];
        int len;
        StringBuilder contentBuffer = new StringBuilder();
        while ((len = reader.read(buf)) != -1) {
            contentBuffer.append(buf, 0, len);
        }
        log.info("接收到的银企回调参数如下: {}", contentBuffer);

        Map<String, String> result = new HashMap<>();
        String[] paramArr = contentBuffer.toString().split("&");
        for (String str : paramArr) {
            int index = str.indexOf("=");
            String key = str.substring(0, index);
            String value = str.substring(index + 1);
            result.put(key, value);
        }
        return JsonUtils.parseObject(JsonUtils.toJsonString(result), CallBackEntity.class);
    }
}



